package com.oasistudio.tk.connector.imp;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TreeMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.oasistudio.tk.connector.TopAPIConnectorHelper;
import com.oasistudio.tk.connector.TopApiRequest;
import com.oasistudio.tk.connector.TopApiResponse;
import com.taobao.tbw.common.TBWContext;

/**
 * 获取淘宝画报的所有频道
 */
public class TaobaoPosterAppointedPostersGetConnector implements TopAPIConnectorHelper {
	private String channelIds;
	public TaobaoPosterAppointedPostersGetConnector() {
	}
	
	public void setChannelIds(String channelIds){
		this.channelIds = channelIds;
	}

	@Override
	public Object asyncPaser(byte[] part) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getApiUrl() {
		TopApiRequest request = new TopApiRequest();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		TreeMap<String, String> params = new TreeMap<String, String>();
		params.put("method", "taobao.poster.appointedposters.get");
		params.put("session", TBWContext.getInstance().getTopsession());
		params.put("timestamp", sdf.format(calendar.getTime()));
		params.put("format", "json");
		params.put("app_key", TBWContext.getInstance().getAppKey());
		params.put("v", "2.0");
		params.put("partner_id", "top-apitools");
		params.put("sign_method", "md5");
		//API method 参数
		params.put("channel_ids", channelIds);
		//params.put("appointed_type", "HOT");
		params.put("appointed_type", "RECOMMEND");
		params.put("re_num", "9");
		request.params = params;
		String url = request.generalRequestUrl(true);
		System.out.println("ItemListConnector.getApiUrl() url = "+url);
		return url;
	}

	/**
	 * 
	 */
	@Override
	public Object syncPaser(byte[] all) {
		PostersInfo postersInfo = new PostersInfo();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			TopApiResponse response = new TopApiResponse("poster_appointedposters_get_response");
			String str = new String(all, "UTF-8");
			//Log.d("return msg:", str);
			if (response.parseResult(str).success) {
				postersInfo.isSuccess = true;
				JSONObject jsObj = response.data;
				if (jsObj.has("appointedposters")) {
					JSONObject itemsJSON = jsObj.getJSONObject("appointedposters");
					if (itemsJSON.has("huabao")) {
						JSONArray postersArray = itemsJSON.getJSONArray("huabao");
						Poster[] posters = new Poster[postersArray.length()];
						postersInfo.posters = posters;
						for (int i = 0; i < postersArray.length(); i++) {
							JSONObject posterJSON = postersArray.getJSONObject(i);
							posters[i] = new Poster();
							posters[i].id = posterJSON.optLong("id");
							posters[i].create_date = sdf.parse(posterJSON.optString("create_date"));
							posters[i].modified_date = sdf.parse(posterJSON.optString("modified_date"));
							posters[i].title = posterJSON.optString("title");
							posters[i].title_short = posterJSON.optString("title_short");
							posters[i].weight = posterJSON.optInt("weight");
							posters[i].hits = posterJSON.optLong("hits");
							posters[i].channel_id = posterJSON.optLong("channel_id");
							posters[i].tag = posterJSON.optString("tag");
							posters[i].cover_pic_url = posterJSON.optString("cover_pic_url");
						}
					}
				}
			} else {
				postersInfo.isSuccess = false;
				postersInfo.errCode = response.errCode;
				postersInfo.errInfo = response.errInfo;
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return postersInfo;
	}

	public static class PostersInfo {
		public String errCode;
		public String errInfo;
		public Boolean isSuccess;
		public Poster[] posters;
	}

	public static class Poster {
		public Number id;
		public Date create_date;//
		public Date modified_date;//
		public String title;
		public String title_short;
		public String tag;
		public Number weight;
		public String cover_pic_url;
		public Number hits;
		public Number channel_id;
		
		public String toString(){
			StringBuilder sb = new StringBuilder();
			sb.append("id:"+id);
			sb.append(";title_short:"+title_short);
			sb.append(";create_date:"+create_date);
			sb.append(";hits:"+hits);
			return sb.toString();
		}
	}
}
